package com.web_bloks.types.client;

//import com.web_bloks.system.Logger;
import java.util.HashMap;

/**
 * 
 * @author Jean-Lou Dupont
 *
 */
public class HashCollections {

	final static String thisClass = "com.web_bloks.types.HashCollections";
	
	static HashMap<String,HashCollections> collections = new HashMap<String,HashCollections>();
	
	/*
	 * INSTANCE RELATED
	 */
	protected HashMap<String,Object> list = new HashMap<String,Object>();
	
	protected String listName = null;
	
	/**
	 * Constructor
	 * @param name
	 */
	protected HashCollections() {
		super();
	}
	
	protected HashCollections(String name) {
		super();
		listName = new String( name );
		//Logger.logInfo(thisClass+".HashCollections()");
	}
	
	public String getName() {
		return listName;
	}
	/**
	 * Creates a widget collection of a given name
	 *  OR returns an existing collection
	 * @param listName
	 * @return
	 */
	public static HashCollections create(String collectionName){
		//Logger.logInfo(thisClass+".create: collectionName: " + collectionName + ", exists["+(collections!=null)+"]" );
		
		HashCollections c = null;
		c = HashCollections.exists( collectionName );
		if ( null != c) {
			//Logger.logInfo(thisClass+".create: FOUND existing collectionName: " + collectionName );
			return c;
		}
		c = new HashCollections( collectionName );
		
		return _createCollection( collectionName, c );
	}
	
	protected static HashCollections _createCollection(String collectionName, HashCollections c) {
		//Logger.logInfo(thisClass+".create: CREATING collectionName: " + collectionName );
		
		collections.put(collectionName, c);
		
		return c;
	}
	
	public static void clear(String collectionName){

		HashCollections c = null;
		c = HashCollections.exists( collectionName );
		if ( null != c ) {
			c.clear();
		}
	}
	
	/**
	 * Verifies if a given collection already exists
	 * 
	 * @param name
	 * @return widgetcollection
	 */
	public static HashCollections exists(String name) {
		//Logger.logInfo(thisClass+".exists: collectionName: " + name + ", exists["+(collections!=null)+"]" );
		
		HashCollections c = null;
		try {
			if ( collections.containsKey(name) )
				c =(HashCollections) collections.get(name);
		} catch(Exception e) {
			c = null;
		}
		
		return c;
	}
	
	public Object get(String key){
		return list.get(key);
	}
	
	public void add(String key, Object w) {
		list.put(key, w);
	}
	
	public void remove(String key) {
		list.remove(key);
	}
	
	public void clear() {
		
		list.clear();
	}
	
	public static void add(String collectionName, String key, Object o) {
		//Logger.logInfo(thisClass+".add: collectionName: " + collectionName + "key["+key+"]");
		HashCollections c = HashCollections.create(collectionName);
		
		c.add(key, o);
	}
	
	public static void remove(String collectionName, String key) {
		HashCollections c = HashCollections.create(collectionName);
		
		c.remove(key);
	}
	
}//END